package com.liang.paul.officelite.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.gitonway.lee.niftymodaldialogeffects.lib.Effectstype;
import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.adapters.TeamListAdapter;
import com.liang.paul.officelite.beams.OfficeUser;
import com.liang.paul.officelite.beams.TeamBeam;
import com.liang.paul.officelite.beams.TeamPersonMapBeam;
import com.shamanland.fab.FloatingActionButton;
import com.shamanland.fab.ShowHideOnScroll;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;

public class TeamActivity extends FastActivity {

    ListView list;
    ArrayList<TeamBeam> datas;
    TeamListAdapter adapter;
    FloatingActionButton add;
    OfficeUser user;
    public static final int ADDTEAM = 101;
    public static final int CREATETEAM = 102;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);
        initView();
        getData();
    }

    private void initView() {
        user = BmobUser.getCurrentUser(this, OfficeUser.class);
        datas = new ArrayList<TeamBeam>();
        list = f(R.id.lv_team_list);
        add = f(R.id.fab_team_add);
        list.setOnTouchListener(new ShowHideOnScroll(add));
        adapter = new TeamListAdapter(this, datas);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TeamActivity.this, TeamDetailActivity.class);
                intent.putExtra("teamId", datas.get(position).getObjectId());
                startActivity(intent);
            }
        });
    }

    private void getData(){
        BmobQuery<TeamPersonMapBeam> query = new BmobQuery<TeamPersonMapBeam>();
        query.addWhereEqualTo("personId", user.getObjectId());
        query.setLimit(50);
        query.findObjects(this, new FindListener<TeamPersonMapBeam>() {
            @Override
            public void onSuccess(List<TeamPersonMapBeam> list) {
                for (TeamPersonMapBeam map : list){
                    BmobQuery<TeamBeam> teamQuery = new BmobQuery<TeamBeam>();
                    teamQuery.getObject(getApplicationContext(), map.getTeamId(), new GetListener<TeamBeam>() {
                        @Override
                        public void onSuccess(TeamBeam teamBeam) {
                            datas.add(teamBeam);
                            adapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void onClick(View view){
        NiftyDialogBuilder builder = NiftyDialogBuilder.getInstance(this);
        builder
                .withTitle("选择操作")
                .withTitleColor("#FFFFFF")
                .withDividerColor("#11000000")
                .withMessage("")
                .withDialogColor("#666666")
                .withEffect(Effectstype.Fall)
                .withButton1Text("加入团队")
                .withButton2Text("建立团队")
                .setButton1Click(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TeamActivity.this, AddTeamActivity.class);
                        startActivityForResult(intent, ADDTEAM);
                    }
                })
                .setButton2Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TeamActivity.this, CreateTeamActivity.class);
                        startActivityForResult(intent, CREATETEAM);
                    }
                })
                .show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ADDTEAM && resultCode == AddTeamActivity.ADDTEAM)
            if(data.getBooleanExtra("isSuccess", false)){
                datas.clear();
                adapter.notifyDataSetChanged();
                getData();
            }
        if(requestCode == CREATETEAM && resultCode == 104)
            if(data.getBooleanExtra("isSuccess", false)){
                datas.clear();
                adapter.notifyDataSetChanged();
                getData();
        }
    }
}
